﻿#region License
/**	
The MIT License (MIT)

Copyright (c) 2013 Philipp Schillinger, Michael Alekseew, Andreas Braun, Steeven Zeiß

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
 **/
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
using System.Text;
using System.Windows.Forms;

namespace IndoorLocalization.UserInterface {
	/// <summary>
	/// Realizes a window that allows the user to customize this application with personal settings.
	/// </summary>
	public partial class PreferencesWindow : Form {

		/// <summary>
		/// Create a new window.
		/// </summary>
		public PreferencesWindow() {
			InitializeComponent();
		}

		/// <summary>
		/// Initialize all window elements with the current settings when loading.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void PreferencesWindow_Load(object sender, EventArgs e) {
			chooseProfileTextbox.Text = Program.ProfilePath;
			thresholdValueBox.Value = (decimal)Math.Round(Program.WallThreshold * 100);
		}

		/// <summary>
		/// Event that is triggered when the user wants to choose a new start-up profile.
		/// Opens a file dialog and waits for a file to be chosen.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void chooseProfileButton_Click(object sender, EventArgs e) {
			OpenFileDialog ofd = new OpenFileDialog();
			ofd.Title = "Please choose a profile to load on startup";
			ofd.Filter = "Profile files|*.profile";
			if (ofd.ShowDialog() == DialogResult.OK) {
				chooseProfileTextbox.Text = ofd.FileName;
				applyButton.Enabled = true;
			}
		}

		/// <summary>
		/// Event that is triggered when the brightness threshold value changes.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void thresholdValueBox_ValueChanged(object sender, EventArgs e) {
			applyButton.Enabled = true;
		}
		/// <summary>
		/// Event that is triggered when the start-up profile changes.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void chooseProfileTextbox_TextChanged(object sender, EventArgs e) {
			applyButton.Enabled = true;
		}

		/// <summary>
		/// Event that is triggered when the apply button is clicked.
		/// Saves all changed settings.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void applyButton_Click(object sender, EventArgs e) {
			applyButton.Enabled = false;
			ApplyChanges();
			Program.SaveSettings();
		}

		/// <summary>
		/// Event that is triggered when the cancel button is clicked.
		/// Discards all changed settings and closes the window.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void cancelButton_Click(object sender, EventArgs e) {
			Close();
		}

		/// <summary>
		/// Event that is triggered when the ok button is clicked.
		/// Saves all changed settings and closes the window.
		/// </summary>
		/// <param name="sender">Not used.</param>
		/// <param name="e">Not used.</param>
		private void okButton_Click(object sender, EventArgs e) {
			ApplyChanges();
			Program.SaveSettings();
			Close();
		}

		/// <summary>
		/// Applies all changed settings by updating the corresponding elements.
		/// </summary>
		private void ApplyChanges() {
			Program.WallThreshold = (float)thresholdValueBox.Value / 100;

			if (File.Exists(chooseProfileTextbox.Text) || chooseProfileTextbox.Text.Equals(""))
				Program.ProfilePath = chooseProfileTextbox.Text;
			else
				MessageBox.Show(
					"Invalid profile file, old path will be kept.",
					"Warning",
					MessageBoxButtons.OK,
					MessageBoxIcon.Warning,
					MessageBoxDefaultButton.Button1
				);
		}

	}
}
